package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.domain.Employee;
import cn.itsource.hrm.domain.Tenant;
import cn.itsource.hrm.dto.TenantDto;
import cn.itsource.hrm.exception.ErrorCode;
import cn.itsource.hrm.mapper.TenantMapper;
import cn.itsource.hrm.service.IEmployeeService;
import cn.itsource.hrm.service.ITenantService;
import cn.itsource.hrm.util.AssertUtils;
import cn.itsource.hrm.util.ValidatorUtils;
import cn.itsource.hrm.util.encrypt.MD5;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.time.DateUtils;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ¿­
 * @since 2021-01-15
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private IEmployeeService employeeService;

    /**
     *
     * 机构入驻
     * @param dto
     */
    @Override
    public void entering(TenantDto dto) {
        Employee employee = dto.getEmployee();
        Tenant tenant = dto.getTenant();
        Long mealId = dto.getMealId();
        // 前端所有的验证都是不可信的
        parmsValidate(employee, tenant, mealId);

        //1.先保存机构 , 防止空值的注入
        Date now = inserTenant(tenant);
        //2. 在保存员工
        insertEmp(employee, tenant, now);

        //3.保存机构和套餐之间的关系
        insertMeal(tenant, mealId, now);

    }

    @Value("${hrm.tenant.regiest.amountDaies}")
    private Integer amount;

    @Value("${hrm.tenant.regiest.saltleng}")
    private Integer saltleng;

    private void insertMeal(Tenant tenant, Long mealId, Date now) {
        //meal_id,tenant_id,expire_date,state
        //0 未支付，1已经购买，2过期
        Date expireDate = DateUtils.addDays(now, amount);

        baseMapper.insertRelationWithMeal(mealId,tenant.getId(),expireDate,Tenant.STATE_NOT_PAY);
    }

    private void insertEmp(Employee employee, Tenant tenant, Date now) {
        employee.setDeptId(null);
        employee.setId(null);
        employee.setInputTime(now);


        // 密码加盐
        String salt = MD5.getRandomCode(saltleng);
        employee.setPassword(MD5.getMD5(employee.getPassword()+salt));
        employee.setSalt(salt);

        employee.setState(Employee.STATE_NORMAL); //0正常，1锁定，2注销
        employee.setTenantId(tenant.getId());
        employee.setType(Employee.TYPE_TENANT_ANMINE); //1平台普通员工 ，2平台客服人员，3平台管理员，4机构员工，5,机构管理员或其他
        employeeService.insert(employee);
    }

    private Date inserTenant(Tenant tenant) {
        tenant.setId(null);
        Date now = new Date();
        tenant.setRegisterTime(now);
        // 后面抽去常量
        tenant.setState(Tenant.PENDING);
        baseMapper.insert(tenant);
        return now;
    }

    private void parmsValidate(Employee employee, Tenant tenant, Long mealId) {
        // 非空验证
        // 公司名不可为空
        AssertUtils.isnotNull(ErrorCode.COMPANY_NOT_EMPTY_101.getMsg(),tenant.getCompanyName());
        // 执照合法性 通过正则表达式
        AssertUtils.isBusinessLicense(ErrorCode.ILLEGAL_BUSINESS_201.getMsg(),tenant.getCompanyNum());
        // 套餐不可为空
        AssertUtils.isnotNull(ErrorCode.MEAL_NOT_EMPTY_102.getMsg(),mealId);
        // 员工账号不可为空
        AssertUtils.isnotNull(ErrorCode.EMPLOYEE_NAME_NOT_EMPTY_103.getMsg(),employee.getUsername());
        // 手机号合法
        AssertUtils.isRegIlegal(ErrorCode.ILLEGAL_PHONE_202.getMsg(),ValidatorUtils.REG_Moblie,employee.getTel());
        // 邮箱合法
        AssertUtils.isRegIlegal(ErrorCode.ILLEGAL_EMAIL_203.getMsg(),ValidatorUtils.REG_EMAil,employee.getEmail());

        // 不可重复
        // 公司名不可重复
        // 员工账号不可重复
        AssertUtils.isTrue(ErrorCode.REPEATED_EMPLOYEE_NAME_301.getMsg(),!employeeService.isexistUserName(employee.getUsername()));
        ;
        // 手机号不可重复
        // ...
    }

    public static void main(String[] args) {
        Assert.assertNotNull("套餐不可为空",null);
    }
}
